package com.eystar.ext.plugin.crate.db;

import java.sql.Connection;
import java.sql.DriverManager;

import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;

import com.jfinal.log.Log4jLog;


public class CratePoolFactory extends BasePooledObjectFactory<Connection>{

	public static final Log4jLog logger = Log4jLog.getLog(CratePoolFactory.class);
	
	@Override
	public Connection create() throws Exception {
		Class.forName(CrateDb.DRIVER);
	    Connection conn = DriverManager.getConnection(CrateDb.URL);
	    if(CrateDb.SHOW_SQL){
	    	CrateSqlReporter.setLogger(CrateDb.SHOW_SQL);
	    	return new CrateSqlReporter(conn).getConnection();
	    }else{
	    	return conn;
	    }
	}

	@Override
	public PooledObject<Connection> wrap(Connection connection) {
		return new DefaultPooledObject<>(connection);
	}
}
